<?php
/*
 *   This file is part of poat.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 * 
 *   Copyright (c) 2008 Sergio Gabriel Teves <gabriel.sgt at gmail.com>
 * 
 */
 
require_once(ROOT_PATH."class/glossary.class.php");
include_once(ROOT_PATH."class/team.class.php");

if ($param['op']=="add") {
	$gl = new glossary;
	$gl->eterm=$param['eterm'];
	$gl->translation=$param['translation'];
	$gl->context=$param['context'];
	$gl->related=$param['related'];
	$gl->comments=$param['comments'];
	$gl->teamid = $param['teamid']!="" ? $param['teamid'] : $CURRENT_USER->teamid;
	$gl->userid=$CURRENT_USER->userid;

	if ($param['id']!="") {
		$old = new glossary;
		$old->id=$param['id'];
		$loader->get($old);

		if ($old->eterm!=$gl->eterm and $loader->getBy($gl,$gl->findByTerm())!==false) {
			$error = $LANG['duplicatedterm'];
			$tm->assign("current",$param);	
		} else {
			$gl->id=$param['id'];
			$loader->update($gl);
			sendGlossaryNotification($gl,$old,$CURRENT_USER);
			$message=$LANG['termupdated'];
		}

	} else {
		if ($loader->getBy($gl,$gl->findByTerm())===false) {
			$loader->save($gl);
			sendGlossaryNotification($gl,null,$CURRENT_USER);
			$message=$LANG['termadded'];	
		} else {
			$error = $LANG['duplicatedterm'];
			$tm->assign("current",$param);	
		}		
	}
	$updatecontent=false;
} elseif ($param['op']=="view") {
	$gl = new glossary;
	$gl->id=$param['id'];
	$loader->get($gl);
	if ($ret===false) {
		$gl->eterm=$param['id'];
		$gl->id=null;
	}
	$tm->assign("current",$gl);
	$tm->assign("teamid",$param['teamid']);
	$content = $tm->fetch("glossaryview.tpl");
} elseif ($param['op']=="edit") {
	$gl = new glossary;
	$gl->id=$param['id'];
	$ret = $loader->get($gl);
	if ($ret===false) {
		$gl->eterm=$param['id'];
		$gl->id=null;
	}
	$tm->assign("current",$gl);
	$tm->assign("teamid",$param['teamid']);
	$content = $tm->fetch("glossaryadd.tpl");	
} elseif ($param['op']=="find") {
	$gl = new glossary;
	$gl->eterm=$param['id'];
	$gl->teamid = $param['teamid']!="" ? $param['teamid'] : $CURRENT_USER->teamid;
	$loader->getBy($gl,$gl->findByTerm());
	$tm->assign("teamid",$param['teamid']);
	$tm->assign("current",$gl);
	$content = $tm->fetch("glossaryview.tpl");
} elseif ($param['op']=="new") {
	$tm->assign("teamid",$param['teamid']);	
	$content = $tm->fetch("glossaryadd.tpl");
} elseif ($param['op']=="jump") {
	$gl = new glossary;
	$gl->teamid = $param['teamid']!="" ? $param['teamid'] : $CURRENT_USER->teamid;
	if ($param['id']=="ALL") {
		$list = $loader->loadList($gl);	
	} else {
		$list = $loader->loadListBy($gl,$gl->listStartsWith($param['id']));
	}
	if (count($list)==0) {
		$content = "<p class=\"error\">".$LANG['listempty']."</p>";
	} else {
		$tm->assign("list",$list);
		$content = $tm->fetch("glossarylist.tpl");	
	}
} elseif ($param['op']=="search") {
	$gl = new glossary;
	$gl->teamid = $param['teamid']!="" ? $param['teamid'] : $CURRENT_USER->teamid;
	$list = $loader->loadListBy($gl,$gl->find($param['id']));
	
	if (count($list)==0) {
		$ar = array($param['id']);
		$error = createMessage($LANG['nothingfound'],$ar);	
	}
	
	$tm->assign("list",$list);
	$content = $tm->fetch("glossarylist.tpl");	
} else {
	$table = array();
	for ($i=ord("A");$i<=ord("Z");$i++) {
		array_push($table,chr($i));	
	}
	array_push($table,"ALL");
	
	if ($CURRENT_USER->groupid==GROUP_ADMIN) {
		$team = new team;
		$team->status=STATUS_ACTIVE;
		$tlist = $loader->loadListBy($team,$team->listByStatus());
		$tm->assign("teamlist",$tlist);
	}
	
	$tm->assign("alfa",$table);
	$content = $tm->fetch("glossary.tpl");
}

?>
